package backstage.app.service.impl.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import backstage.app.entity.NoStudent;
import backstage.app.mapper.NoStudentMapper;
import backstage.app.respont.RespontDateJson;

@Component
public class NoStudentImplUtil {
	
	 private static final Logger logger = (Logger) LoggerFactory.getLogger(NoStudentImplUtil.class);
	
	 @Autowired
	 private NoStudentMapper noStudentMapper;
	public RespontDateJson<?> inser( String name, String sex, String date, String phone,Integer studentType
			)    {
		 if(StringUtils.isEmpty(name)){
             return RespontDateJson.fuall("name="+name,"名字不能为空");
         }
         if( StringUtils.isEmpty(phone)){
             return RespontDateJson.fuall("phone="+phone,"电话不能为空");
         } 
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		NoStudent student=new NoStudent();
		student.setName(name);
		 
		student.setSex(sex);
		 
		
		 
		try {
			student.setBirthday(sdf.parse(date));
			String[] ages=date.split("-");
			String nowtime=LocalDateTime.now().toString();
			String []nowage=nowtime.split("-"); 
			int old=Integer.parseInt(nowage[0]);
			int young=Integer.parseInt(ages[0].toString());
			int age= old-young;
			student.setAge(age);
		}catch (ParseException e) {
			 logger.error("类型转换异常");
		}
		//计算年龄
		student.setPhone(phone);
		student.setExportFlg(1);
		student.setCreateTime(LocalDateTime.now());
		student.setLastUpdateTime(LocalDateTime.now());
		student.setStudentType(studentType);
		int index=noStudentMapper.insertSelective(student);
		
		
		return index>0?RespontDateJson.ok(index, "添加成功"):RespontDateJson.fuall(index, "添加失败");
	}
	
	
	
	public RespontDateJson<?> update(Integer sid, String name, String sex, String birthday, String phone,Integer studentType
			) throws ParseException    {  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        NoStudent student=new NoStudent();
		student.setSid(sid);
		student.setName(name);
		student.setSex(sex);
		student.setBirthday(sdf.parse(birthday));
		/**避免发生空指针**/
		if(birthday!=null) {
			try {
				student.setBirthday(sdf.parse(birthday));
				String[] ages=birthday.split("-");
				String nowtime=LocalDateTime.now().toString();
				String []nowage=nowtime.split("-"); 
				int old=Integer.parseInt(nowage[0]);
				int young=Integer.parseInt(ages[0].toString());
				int age= old-young;
				student.setAge(age);
			}catch (ParseException e) {
				 logger.error("类型转换异常");
			}
		}	 
		//计算年龄
		student.setPhone(phone);
		 
		student.setLastUpdateTime(LocalDateTime.now());
		student.setStudentType(studentType);
		int index=noStudentMapper.updateByPrimaryKey(student);	
		return index>0?RespontDateJson.ok(index, "修改成功"):RespontDateJson.fuall(index,"修改失败");
	}
	
}
